package com.hrhx.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

/**
 * 字符串处理
 * @author Administrator
 *
 */
public class StringUtil {
	
	/**
	 * 得到文件的后缀名
	 * @param fileName
	 * @return
	 */
	public static String getFileExtension(String fileName){
		return fileName.substring(fileName.lastIndexOf("."), fileName.length());
	}

	/**
	 * 字符串字符转义
	 * @return
	 */
	public static String replaceStr(String queryWord) {
		queryWord = queryWord.replaceAll("'","\'");// 防止查询中带'报错
		queryWord = queryWord.replaceAll("\r","\\r");// 防止查询中带\r报错
		queryWord = queryWord.replaceAll("\n","\\n");// 防止查询中带\n报错
		queryWord = queryWord.replaceAll("\t","\\t");// 防止查询中带\t报错
		queryWord = queryWord.replaceAll(":","\\:");// 防止查询中带:报错
		//queryWord = queryWord.replaceAll("\\\\","\\\\");// 防止查询中带\\报错
		return queryWord.trim();
	}

	/**
	 * 强制输出到页面
	 * @param response
	 * @param tips
	 * @throws IOException
	 */
	public static void outPageByHtml(HttpServletResponse response,String tips){
		//如果中文返回出现？？字符
		response.setCharacterEncoding("UTF-8");
		//如果返回的中文是“烇湫”这种乱码，说明浏览器的解析问题
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		try {
			PrintWriter out = response.getWriter();
			out.print(tips);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void outPageByJson(HttpServletResponse response,String tips){
		//如果中文返回出现？？字符
		response.setCharacterEncoding("UTF-8");
		//如果返回的中文是“烇湫”这种乱码，说明浏览器的解析问题
		response.setHeader("Content-type", "text/json;charset=UTF-8");
		try {
			PrintWriter out = response.getWriter();
			out.print(tips);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void outPageByXml(HttpServletResponse response,String tips){
		//如果中文返回出现？？字符
		response.setCharacterEncoding("UTF-8");
		//如果返回的中文是“烇湫”这种乱码，说明浏览器的解析问题
		response.setHeader("Content-type", "text/xml;charset=UTF-8");
		try {
			PrintWriter out = response.getWriter();
			out.print(tips);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
